import { _decorator, Component, Node, UIOpacity, tween, Label } from 'cc';
import { BasicUI } from '../../Init/Basic/BasicUI';
import { GlobalTmpData } from '../../Init/Config/GlobalTmpData';
import { EventTypes } from '../../Init/Managers/EventTypes';
import { AudioEnum } from '../../Init/SystemAudio/AudioEnum';
import { AudioSystem } from '../../Init/SystemAudio/AudioSystem';
import { SDKSystem, PlatformType } from '../../Init/SystemSDK/SDKSystem';
import { StorageSystem } from '../../Init/SystemStorage/StorageSystem';
import { UIEnum } from '../../Init/SystemUI/UIEnum';
import { UISystem } from '../../Init/SystemUI/UISystem';
import Loader from '../../Init/Tools/Loader';
import { Adsdk } from '../../myScript/Adsdk';
const { ccclass, property } = _decorator;

@ccclass('HomeUI')
export class HomeUI extends BasicUI {
    @property(Node)
    protected panel: Node = null;

    @property(Node)
    private privacyBtn: Node = null;

    @property(Node)
    private teachAnim: Node = null;

    @property(Node)
    private loading: Node = null;

    @property(Node)
    private touchBtn: Node = null;

    @property(Node)
    private settingBtn: Node = null;

    private isEnter = false;
    private isFirstLoad = true;

    protected onEvents() {
        this.on(EventTypes.GameEvents.GameLoadFinish, this.onGameLoadFinish, this);
        this.on(EventTypes.UIEvents.PrivacyConfirm, this.onPrivacyConfirm, this);
        this.on(EventTypes.UIEvents.EnterGame, this.onEnterGame, this);

    }

    public show(d) {
        super.show(d);

        this.isEnter = false;

        // 引导关卡不需要显示UI内容
        let lv = StorageSystem.getData().levelAssets.curLv;
        let isGuideLv = lv == 1;
        //
        let bgm = lv % 2 == 0 ? AudioEnum.Bgm1 : AudioEnum.Bgm2;
        AudioSystem.playBGM(bgm);

        this.touchBtn.active = isGuideLv;
        this.teachAnim.active = true;
        this.panel.active = true;
        this.loading.active = false;
        //
        UISystem.showUI(UIEnum.PlayerAssetsUI);
        UISystem.showUI(UIEnum.MapUI);

        this.setLvNum();

        this.privacyBtn.active = false;
        if (SDKSystem._curPlatform == PlatformType.OPPOMiniGame ||
            SDKSystem._curPlatform == PlatformType.VIVOMiniGame ||
            SDKSystem._curPlatform == PlatformType.PCMiniGame) {
            this.privacyBtn.active = true;

            //检测隐私政策是否已同意
            if (!StorageSystem.getData().userSetting.showPrivacy) {
                //首页显示关卡内容->自动进入游戏
                // this.enterGame();
            }
        } else {
            //首页显示关卡内容->自动进入游戏            
            // this.enterGame();     

        }

        if (SDKSystem._curPlatform == PlatformType.TTMiniGame) {
            this.settingBtn.active = false;
        }

        this.bgNode.active = true;
        //第一次进入游戏且是引导关卡时-
        if (this.isFirstLoad && isGuideLv) {
            //不直接显示游戏场景
            this.loading.active = false;
        } else {
            //直接显示游戏场景
            this.enterGame();
            this.loading.active = true;
        }

        this.isFirstLoad = false;

        Adsdk.instance.hideAllAd();
        Adsdk.instance.showInterstitialAd();
        Adsdk.instance.showBanner();
        Adsdk.instance.showGridAdRightOne();
        Adsdk.instance.showGridAdLeftOne();
    }

    //点击开始
    public onTouchStart() {
        if (this.panel.active) {
            this.panel.active = false;
            this.loading.active = true;
            this.enterGame();

            UISystem.hideUI(UIEnum.MapUI);
        }
    }

    //进入游戏
    public enterGame() {
        if (this.isEnter) return;
        this.isEnter = true;

        this.emit(EventTypes.GameEvents.GameStart, () => {
            setTimeout(() => {
                this.emit(EventTypes.GameEvents.GameLoadFinish);
            }, 0);
        });

        Adsdk.instance.hideAllAd();
    }

    public hide() {
        super.hide();
        this.bgNode.active = false;
        // UISystem.hideUI(UIEnum.PlayerAssetsUI);
    }

    // #region -----------私有-----------

    @property(Node)
    protected bgNode: Node = null;
    //隐藏背景动画
    private hideBg(cb?) {
        if (!this.bgNode.active) {
            cb && cb();
            return;
        };
        let opCmp = this.bgNode.getComponent(UIOpacity);
        if (opCmp) {
            let o = { a: 255 }
            tween(o).to(1, { a: 0 }, {
                onUpdate: () => {
                    opCmp.opacity = o.a;
                }
            }).call(() => {
                this.bgNode.active = false;
                cb && cb();
            }).start();
        }
    }

    @property(Label)
    protected lvLabel: Label = null;
    private setLvNum() {
        let lv = StorageSystem.getData().levelAssets.curLv;
        this.lvLabel.string = lv.toFixed(0);
    }

    // #endregion

    // #region -----------------按钮--------------
    /**点击开始按钮 */
    public onGameStartClick() {
        AudioSystem.playEffect(AudioEnum.BtnClick);
        UISystem.hideUI(UIEnum.HomeUI);

        this.emit(EventTypes.GameEvents.GameStart, () => {
            //显示关卡信息UI
            UISystem.showUI(UIEnum.LevelInfoUI);
            //显示触摸
            UISystem.showUI(UIEnum.LevelController);
        });
    }

    /**显示转盘 */
    protected onShowTurntableUI() {
        AudioSystem.playEffect(AudioEnum.BtnClick);
        UISystem.showUI(UIEnum.TurntableUI);
    }
    /**显示签到 */
    protected onShowSignUI() {
        AudioSystem.playEffect(AudioEnum.BtnClick);
        UISystem.showUI(UIEnum.SignUI);
    }

    /**显示设置 */
    protected onShowSettingUI() {
        AudioSystem.playEffect(AudioEnum.BtnClick);
        UISystem.showUI(UIEnum.SettingUI);
    }

    //隐私
    protected onShowPrivacyUI() {
        UISystem.showUI(UIEnum.PrivacyUI, { isLobby: true });
    }

    // #endregion

    // #region ---------------事件------------
    protected onEnterGame() {
        this.onTouchStart();
    }

    //隐私政策同意之后 进入游戏
    protected onPrivacyConfirm() {
        // if (StorageSystem.getData().levelAssets.curLv == 1) {
        //     this.enterGame();
        // }
    }

    //关卡内容加载完毕之后
    protected onGameLoadFinish() {
        //显示触摸
        UISystem.showUI(UIEnum.LevelController);
        //隐藏首页背景
        this.hideBg(() => {
            let lv = StorageSystem.getData().levelAssets.curLv;
            if (lv == 1) {
                // UISystem.hideUI(UIEnum.HomeUI);
                this.loading.active = false;
            }
        });
    }

    // #endregion

}

